﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using HYCodeBase.WinForm.HookSuit;
using HYCodeBase.WinForm.WinFormRemindSuit;
using HYRevitCode.FormUtility;
using QuickModel.Basic;
using QuickModel.Models;

namespace QuickModel.Forms
{
	public partial class TerminalFastModelingForm : System.Windows.Forms.Form
	{
		public bool IsLeaveDlg
		{
			get
			{
				return this.m_bIsLeaveDlg;
			}
			set
			{
				this.m_bIsLeaveDlg = value;
			}
		}

		public QuickModelingPara ViewParas
		{
			get
			{
				return this.m_ViewParas;
			}
			set
			{
				this.m_ViewParas = value;
			}
		}

		public List<SpecialtyEquipData> AllSpecialtyEquipDatas
		{
			get
			{
				return this.m_AllSpecialtyEquipDatas;
			}
			set
			{
				this.m_AllSpecialtyEquipDatas = value;
			}
		}

		public string Speciatly
		{
			get
			{
				return this.m_Speciatly;
			}
			set
			{
				this.m_Speciatly = value;
			}
		}

		public string EquipType
		{
			get
			{
				return this.m_EquipType;
			}
			set
			{
				this.m_EquipType = value;
			}
		}

		public string PublishGuid
		{
			get
			{
				return this.m_PublishGuid;
			}
			set
			{
				this.m_PublishGuid = value;
			}
		}

		public TerminalFastModelingForm()
		{
			this.InitializeComponent();
		}

		public TerminalFastModelingForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.InitView();
			this.m_Doc = doc;
		}

		public TerminalFastModelingForm(Autodesk.Revit.DB.Document doc, string speciatly, string equipType)
		{
			this.InitializeComponent();
			this.m_Speciatly = speciatly;
			this.m_EquipType = equipType;
			this.InitView();
			this.m_Doc = doc;
		}

		private void TerminalFastModelingForm_Load(object sender, EventArgs e)
		{
			this.m_PublishGuid = Guid.NewGuid().ToString();
			base.Icon = Resource.YArchitechIcon;
			this.InitData();
			List<KeyValuePair<ControlPacker, bool>> lastViewDatas = this.GetLastViewDatas(this.m_Speciatly, this.m_EquipType);
			this.m_ctrlManager = new ControlInfoManager(this, lastViewDatas, this.m_EquipType, null);
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.SetControlInfoToForm();
			}
			this.m_hook = new MouseHook(this);
			this.m_hook.HookStart();
		}

		private void m_cmbSpecialty_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void m_cmbEquip_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void Uctrl_FamilySymbolChanged(object sender, EventArgs e)
		{
			this.m_bDoHook = true;
			this.m_PublishGuid = Guid.NewGuid().ToString();
		}

		private void m_rbtnWholeLevel_CheckedChanged(object sender, EventArgs e)
		{
			this.m_ViewParas.IsWholeLevel = this.m_rbtnWholeLevel.Checked;
			this.m_bDoHook = true;
			this.m_PublishGuid = Guid.NewGuid().ToString();
		}

		private void TerminalFastModelingForm_MouseLeave(object sender, EventArgs e)
		{
		}

		private void TerminalFastModelingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.GetControlInfoToXml();
			}
		}

		private void TerminalFastModelingForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			bool flag = this.m_hook != null;
			if (flag)
			{
				this.m_hook.HookStop();
			}
			int wMsg = 7;
			WindowsApi.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			WindowsApi.SendEsc();
			WindowsApi.SendEsc();
		}

		private void TerminalFastModelingForm_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					bool flag = !this.m_bIsLeaveDlg;
					if (flag)
					{
						base.Close();
					}
				}
			}
			catch
			{
			}
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg != 1125)
			{
				base.DefWndProc(ref m);
			}
			else
			{
				bool flag = this.m_bDoHook && !this.m_bIsLeaveDlg;
				if (flag)
				{
					this.MouseLeaveForm();
				}
			}
		}

		private void MouseLeaveForm()
		{
			bool flag = !base.ClientRectangle.Contains(base.PointToClient(Cursor.Position));
			if (flag)
			{
				bool flag2 = !this.Checking();
				if (flag2)
				{
					WindowsApi.SetCursorPos(base.Location.X + base.Width / 2, base.Location.Y + base.Height / 2);
					WindowsApi.SetActiveWindow(base.Handle);
					base.Activate();
					base.Focus();
				}
				else
				{
					int wMsg = 7;
					WindowsApi.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					WindowsApi.SendEsc();
					this.m_bIsLeaveDlg = true;
					this.m_bDoHook = false;
				}
			}
			else
			{
				WindowsApi.SetActiveWindow(base.Handle);
				base.Activate();
				base.Focus();
			}
		}

		private void InitView()
		{
			base.Width = 214;
			this.panel1.Height = 10;
			this.m_lblSpecialty.Visible = false;
			this.m_cmbSpecialty.Visible = false;
			this.Text = this.m_EquipType + "快模";
		}

		public List<KeyValuePair<ControlPacker, bool>> GetLastViewDatas(string speciatly, string equipType)
		{
			List<KeyValuePair<ControlPacker, bool>> list = new List<KeyValuePair<ControlPacker, bool>>();
			bool flag = this.panel3.Controls.Count <= 0;
			List<KeyValuePair<ControlPacker, bool>> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnWholeLevel,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnArea,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				string equipType2 = speciatly + "@" + equipType;
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.panel3.Controls[0],
					UseCustomFormatter = new LastModelControlInfoTransform(equipType2)
				}, true));
				result = list;
			}
			return result;
		}

		private void InitUctrl(string speciatly, EquipData selectedEquip)
		{
			base.Height = 621;
			this.panel3.Controls.Clear();
			string a = speciatly + "@" + selectedEquip.Name;
			bool flag = a == "给排水@消火栓" || a == "给排水@灭火器";
			if (flag)
			{
				UCtrlHydrant uctrlHydrant = new UCtrlHydrant(this.m_Doc, speciatly, selectedEquip);
				uctrlHydrant.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
				this.panel3.Controls.Add(uctrlHydrant);
				uctrlHydrant.Dock = DockStyle.Fill;
			}
			else
			{
				bool flag2 = a == "暖通@风口";
				if (flag2)
				{
					UCtrlWindMouth uctrlWindMouth = new UCtrlWindMouth(this.m_Doc, speciatly, selectedEquip);
					uctrlWindMouth.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
					this.panel3.Controls.Add(uctrlWindMouth);
					uctrlWindMouth.Dock = DockStyle.Fill;
				}
				else
				{
					bool flag3 = a == "暖通@风机盘管";
					if (flag3)
					{
						UCtrlDraughtFanPipe uctrlDraughtFanPipe = new UCtrlDraughtFanPipe(this.m_Doc, speciatly, selectedEquip);
						uctrlDraughtFanPipe.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
						this.panel3.Controls.Add(uctrlDraughtFanPipe);
						uctrlDraughtFanPipe.Dock = DockStyle.Fill;
						base.Height = 700;
					}
					else
					{
						bool flag4 = a == "暖通@室内机";
						if (flag4)
						{
							UCtrlInDoorUnit uctrlInDoorUnit = new UCtrlInDoorUnit(this.m_Doc, speciatly, selectedEquip);
							uctrlInDoorUnit.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
							this.panel3.Controls.Add(uctrlInDoorUnit);
							uctrlInDoorUnit.Dock = DockStyle.Fill;
							base.Height = 700;
						}
						else
						{
							bool flag5 = a == "暖通@散热器";
							if (flag5)
							{
								UCtrlRaditor uctrlRaditor = new UCtrlRaditor(this.m_Doc, speciatly, selectedEquip);
								uctrlRaditor.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
								this.panel3.Controls.Add(uctrlRaditor);
								uctrlRaditor.Dock = DockStyle.Fill;
								base.Height = 700;
							}
							else
							{
								UCtrlElectric uctrlElectric = new UCtrlElectric(this.m_Doc, speciatly, selectedEquip);
								uctrlElectric.FamilySymbolChanged += this.Uctrl_FamilySymbolChanged;
								this.panel3.Controls.Add(uctrlElectric);
								uctrlElectric.Dock = DockStyle.Fill;
							}
						}
					}
				}
			}
		}

		private void InitData()
		{
			this.m_cmbSpecialty.Items.Clear();
			this.m_cmbEquip.Items.Clear();
			this.panel3.Controls.Clear();
			this.m_AllSpecialtyEquipDatas = SpecialtyXml.ReadDatasFromXml();
			bool flag = this.m_AllSpecialtyEquipDatas == null || this.m_AllSpecialtyEquipDatas.Count <= 0;
			if (!flag)
			{
				EquipData equipData = this.GetEquipData(this.m_AllSpecialtyEquipDatas, this.m_Speciatly, this.m_EquipType);
				bool flag2 = equipData == null;
				if (!flag2)
				{
					this.m_ViewParas.SelectedEquipData = equipData;
					this.m_ViewParas.SpecialtyName = this.m_Speciatly;
					this.InitUctrl(this.m_Speciatly, equipData);
				}
			}
		}

		private List<string> GetSpecialties(List<SpecialtyEquipData> datas)
		{
			bool flag = datas == null || datas.Count <= 0;
			List<string> result;
			if (flag)
			{
				result = new List<string>();
			}
			else
			{
				List<string> list = new List<string>();
				foreach (SpecialtyEquipData specialtyEquipData in datas)
				{
					bool flag2 = specialtyEquipData != null && !string.IsNullOrEmpty(specialtyEquipData.Specialty) && !list.Contains(specialtyEquipData.Specialty);
					if (flag2)
					{
						list.Add(specialtyEquipData.Specialty);
					}
				}
				result = list;
			}
			return result;
		}

		private List<EquipData> GetEquipDatas(List<SpecialtyEquipData> datas, string specialty)
		{
			bool flag = datas == null || datas.Count <= 0 || string.IsNullOrEmpty(specialty);
			List<EquipData> result;
			if (flag)
			{
				result = new List<EquipData>();
			}
			else
			{
				foreach (SpecialtyEquipData specialtyEquipData in datas)
				{
					bool flag2 = specialtyEquipData != null && !string.IsNullOrEmpty(specialtyEquipData.Specialty) && specialtyEquipData.Specialty.Trim() == specialty.Trim() && specialtyEquipData.Equips != null;
					if (flag2)
					{
						return specialtyEquipData.Equips;
					}
				}
				result = new List<EquipData>();
			}
			return result;
		}

		private EquipData GetEquipData(List<SpecialtyEquipData> datas, string specialty, string equipType)
		{
			bool flag = datas == null || datas.Count <= 0 || string.IsNullOrEmpty(specialty);
			EquipData result;
			if (flag)
			{
				result = null;
			}
			else
			{
				foreach (SpecialtyEquipData specialtyEquipData in datas)
				{
					bool flag2 = specialtyEquipData != null && !string.IsNullOrEmpty(specialtyEquipData.Specialty) && specialtyEquipData.Specialty.Trim() == specialty.Trim();
					if (flag2)
					{
						return specialtyEquipData.GetEquipData(equipType);
					}
				}
				result = null;
			}
			return result;
		}

		private List<string> GetEquipNames(List<SpecialtyEquipData> datas, string specialty)
		{
			bool flag = datas == null || datas.Count <= 0 || string.IsNullOrEmpty(specialty);
			List<string> result;
			if (flag)
			{
				result = new List<string>();
			}
			else
			{
				List<string> list = new List<string>();
				foreach (SpecialtyEquipData specialtyEquipData in datas)
				{
					bool flag2 = specialtyEquipData != null && !string.IsNullOrEmpty(specialtyEquipData.Specialty) && specialtyEquipData.Specialty.Trim() == specialty.Trim();
					if (flag2)
					{
						return specialtyEquipData.GetEquipNames();
					}
				}
				result = new List<string>();
			}
			return result;
		}

		private void LoadComboBoxDatas(System.Windows.Forms.ComboBox cmb, List<string> datas)
		{
			bool flag = cmb != null && datas != null && datas.Count > 0;
			if (flag)
			{
				cmb.Items.Clear();
				foreach (string item in datas)
				{
					cmb.Items.Add(item);
				}
			}
		}

		public QuickModelingPara GetViewParas()
		{
			bool flag = this.m_ViewParas.SelectedEquipData == null || this.panel3.Controls.Count <= 0;
			QuickModelingPara result;
			if (flag)
			{
				result = null;
			}
			else
			{
				string a = this.m_ViewParas.SpecialtyName + "@" + this.m_ViewParas.SelectedEquipData.Name;
				bool flag2 = a == "给排水@消火栓" || a == "给排水@灭火器";
				if (flag2)
				{
					UCtrlHydrant uctrlHydrant = this.panel3.Controls[0] as UCtrlHydrant;
					bool flag3 = uctrlHydrant != null;
					if (flag3)
					{
						this.m_ViewParas.ChildViewPara = uctrlHydrant.GetViewParas();
					}
				}
				else
				{
					bool flag4 = a == "暖通@风口";
					if (flag4)
					{
						UCtrlWindMouth uctrlWindMouth = this.panel3.Controls[0] as UCtrlWindMouth;
						bool flag5 = uctrlWindMouth != null;
						if (flag5)
						{
							this.m_ViewParas.ChildViewPara = uctrlWindMouth.GetViewParas();
						}
					}
					else
					{
						bool flag6 = a == "暖通@风机盘管";
						if (flag6)
						{
							UCtrlDraughtFanPipe uctrlDraughtFanPipe = this.panel3.Controls[0] as UCtrlDraughtFanPipe;
							bool flag7 = uctrlDraughtFanPipe != null;
							if (flag7)
							{
								this.m_ViewParas.ChildViewPara = uctrlDraughtFanPipe.GetViewParas();
							}
						}
						else
						{
							bool flag8 = a == "暖通@室内机";
							if (flag8)
							{
								UCtrlInDoorUnit uctrlInDoorUnit = this.panel3.Controls[0] as UCtrlInDoorUnit;
								bool flag9 = uctrlInDoorUnit != null;
								if (flag9)
								{
									this.m_ViewParas.ChildViewPara = uctrlInDoorUnit.GetViewParas();
								}
							}
							else
							{
								bool flag10 = a == "暖通@散热器";
								if (flag10)
								{
									UCtrlRaditor uctrlRaditor = this.panel3.Controls[0] as UCtrlRaditor;
									bool flag11 = uctrlRaditor != null;
									if (flag11)
									{
										this.m_ViewParas.ChildViewPara = uctrlRaditor.GetViewParas();
									}
								}
								else
								{
									UCtrlElectric uctrlElectric = this.panel3.Controls[0] as UCtrlElectric;
									bool flag12 = uctrlElectric != null;
									if (flag12)
									{
										this.m_ViewParas.ChildViewPara = uctrlElectric.GetViewParas();
									}
								}
							}
						}
					}
				}
				result = this.m_ViewParas;
			}
			return result;
		}

		public bool Checking()
		{
			bool flag = true;
			string empty = string.Empty;
			bool flag2 = this.m_ViewParas.SelectedEquipData == null || this.panel3.Controls.Count <= 0;
			bool result;
			if (flag2)
			{
				result = true;
			}
			else
			{
				string a = this.m_ViewParas.SpecialtyName + "@" + this.m_ViewParas.SelectedEquipData.Name;
				bool flag3 = a == "给排水@消火栓" || a == "给排水@灭火器";
				if (flag3)
				{
					UCtrlHydrant uctrlHydrant = this.panel3.Controls[0] as UCtrlHydrant;
					bool flag4 = uctrlHydrant != null;
					if (flag4)
					{
						flag = uctrlHydrant.Check(out empty);
					}
				}
				else
				{
					bool flag5 = a == "暖通@风口";
					if (flag5)
					{
						UCtrlWindMouth uctrlWindMouth = this.panel3.Controls[0] as UCtrlWindMouth;
						bool flag6 = uctrlWindMouth != null;
						if (flag6)
						{
							flag = uctrlWindMouth.Check(out empty);
						}
					}
					else
					{
						bool flag7 = a == "暖通@风机盘管";
						if (flag7)
						{
							UCtrlDraughtFanPipe uctrlDraughtFanPipe = this.panel3.Controls[0] as UCtrlDraughtFanPipe;
							bool flag8 = uctrlDraughtFanPipe != null;
							if (flag8)
							{
								flag = uctrlDraughtFanPipe.Check(out empty);
							}
						}
						else
						{
							bool flag9 = a == "暖通@室内机";
							if (flag9)
							{
								UCtrlInDoorUnit uctrlInDoorUnit = this.panel3.Controls[0] as UCtrlInDoorUnit;
								bool flag10 = uctrlInDoorUnit != null;
								if (flag10)
								{
									flag = uctrlInDoorUnit.Check(out empty);
								}
							}
							else
							{
								bool flag11 = a == "暖通@散热器";
								if (flag11)
								{
									UCtrlRaditor uctrlRaditor = this.panel3.Controls[0] as UCtrlRaditor;
									bool flag12 = uctrlRaditor != null;
									if (flag12)
									{
										flag = uctrlRaditor.Check(out empty);
									}
								}
								else
								{
									UCtrlElectric uctrlElectric = this.panel3.Controls[0] as UCtrlElectric;
									bool flag13 = uctrlElectric != null;
									if (flag13)
									{
										flag = uctrlElectric.Check(out empty);
									}
								}
							}
						}
					}
				}
				bool flag14 = !flag;
				if (flag14)
				{
					YJKMessageBox.Show(empty);
				}
				result = flag;
			}
			return result;
		}

		private ControlInfoManager m_ctrlManager = null;

		private MouseHook m_hook = null;

		public bool m_bDoHook = false;

		private Document m_Doc;

		private bool m_bIsLeaveDlg = false;

		private QuickModelingPara m_ViewParas = new QuickModelingPara();

		private List<SpecialtyEquipData> m_AllSpecialtyEquipDatas;

		private string m_Speciatly;

		private string m_EquipType;

		private string m_PublishGuid;
	}
}
